$(document).ready(function(){
    var copylink = new ClipboardJS('#copylink');
    copylink.on('success', function(e) {
        mviewToastr("success","链接复制成功，请粘贴获取链接!");
    });
});

$(function() {  
    $("#mini_detial li").click(function() {
        $("#mini_detial li").removeClass("active");
        $(this).addClass("active");
        var data = $(this).attr("data");
        $("#record").hide();
        $("#cahrt").hide();
        if (data == "record") {
            $("#record").show();
        } else {
            $("#cahrt").show();
            miniDetialStatic();
        }
    });
});

function miniDetialStatic() {
    var chartDom = document.getElementById('overview-chart');
    var myChart = echarts.init(chartDom);
    myChart.clear();
    var link_id = $("#link_id").val();
    var option;
    $.ajax({
        url: "/admin/alipay/mini_static",
        type: "GET",
        data: {
            "link_id":link_id,
        },
        success:function(result){
            if (result.status == "success") {
                var dayList = [];
                var dataList = [];
                var miniStatics = result.data.miniStatics;
                for (var i = 30; i >= 0; i--) {
                    var date = moment().subtract(i, "days").format("MM月DD日");
                    var day = moment().subtract(i, "days").format("YYYYMMDD");
                    var index = miniStatics.findIndex(miniStatic => {
                        return miniStatic.day == day;
                    });
                    if (index >= 0) {
                        dataList.push(miniStatics[index].pv);
                    } else {
                        dataList.push(0);
                    }
                    dayList.push(date);
                }
                option = {
                    title: {
                    text: '近30天访问统计'
                  },
                  tooltip: {
                    trigger: 'axis'
                  },
                  xAxis: {
                    type: 'category',
                    data: dayList
                  },
                  yAxis: {
                    type: 'value'
                  },
                  series: [
                    {
                      data: dataList,
                      type: 'line',
                      smooth: true
                    }
                  ]
                };

                option && myChart.setOption(option);
                myChart.resize();
            } else {
                mviewToastr("error",result.msg);
            }
        },
        error:function(err){
            if (err.status == 419) {
                mviewToastr("error","页面已过期,请刷新网页后重新保存！");
            } else {
                mviewToastr("error","网络异常,请联系客服处理！");
            }
        }
    })
}